package com.zenith.edu.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zenith.edu.mapper.EduClassMapper;
import com.zenith.edu.po.domain.EduClass;
import com.zenith.edu.po.dto.EduClassDTO;
import com.zenith.edu.po.vo.EduClassVO;
import com.zenith.edu.service.EduClassService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class EduClassServiceImpl extends ServiceImpl<EduClassMapper, EduClass> implements EduClassService {
    
    @Override
    public EduClassVO createClass(EduClassDTO dto) {
        EduClass clazz = new EduClass();
        BeanUtils.copyProperties(dto, clazz);
        this.save(clazz);
        
        EduClassVO vo = new EduClassVO();
        BeanUtils.copyProperties(clazz, vo);
        return vo;
    }
    
    @Override
    public EduClassVO getClassById(Long id) {
        EduClass clazz = this.getById(id);
        if (clazz == null) {
            return null;
        }
        
        EduClassVO vo = new EduClassVO();
        BeanUtils.copyProperties(clazz, vo);
        return vo;
    }
    
    @Override
    public EduClassVO updateClass(EduClassDTO dto) {
        EduClass clazz = new EduClass();
        BeanUtils.copyProperties(dto, clazz);
        this.updateById(clazz);
        
        EduClassVO vo = new EduClassVO();
        BeanUtils.copyProperties(clazz, vo);
        return vo;
    }
    
    @Override
    public boolean deleteClass(Long id) {
        return this.removeById(id);
    }
    
    @Override
    public List<EduClassVO> getClassList() {
        List<EduClass> classList = this.list();
        return classList.stream().map(clazz -> {
            EduClassVO vo = new EduClassVO();
            BeanUtils.copyProperties(clazz, vo);
            return vo;
        }).collect(Collectors.toList());
    }
}